package com.sx.micowell.module.homePage

import android.content.res.ColorStateList
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.SPUtils
import com.google.android.material.tabs.TabLayout
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.response.getUrl
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragHomeViewBinding
import com.sx.micowell.framework.app.MyApplication
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.entity.AccountBean
import com.sx.micowell.framework.model.Home2Model
import com.sx.micowell.framework.tools.getTextView
import com.sx.micowell.module.MainActivity
import com.sx.widget.view.flyco.tablayout.listener.OnTabSelectListener
import kotlinx.android.synthetic.main.frag_home.*
import kotlinx.android.synthetic.main.frag_home_view.*
import kotlinx.android.synthetic.main.frag_recommend_course.*

/**
 * @Author: YY
 * @Time: 2021/9/7
 * @Describe 首页
 */
@Route(path = RoutePath.HOME)
class Home2Fragment : MyFragment<FragHomeViewBinding, MainActivity>() {

    private lateinit var viewModel: Home2Model

    private lateinit var player: PlayerView

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getActivityScopeViewModel(Home2Model::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_home_view, BR.viewModel, viewModel)
            .addBindingParam(BR.manager, childFragmentManager)
            .addBindingParam(BR.context, this)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(false)
            .fitsSystemWindows(false).init()

        intTab()
    }

    override fun initData() {
        initVideoPlayer()
        LogUtils.d("Account: "+SPUtils.getInstance().getString(SPKey.SP_ACCOUNT))
    }

    override fun initViewObservable() {
        super.initViewObservable()

        viewModel.homeVideoData.observe(this, {
            startVideoPlayer(
                it.info.url.getUrl(),
                it.info.cover.getUrl()
            )
            player.setFullListener {
                RouteManager.getInstance().intentMkVideoVideo(it.info.url.getUrl(),it.info.cover.getUrl())
            }
        })
    }


    private fun intTab() {
        // Tab监听器
        binding.tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

            override fun onTabSelected(tab: TabLayout.Tab) {
                val view = tab.customView
                if (null == view) {
                    tab.setCustomView(R.layout.view_tab_text)
                }
                val textView: TextView = tab.customView!!.findViewById(android.R.id.text1)
                textView.setTextAppearance(R.style.TabLayoutTextSize);
                binding.homeVp.currentItem = tab.position
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                val view = tab.customView
                if (null == view) {
                    tab.setCustomView(R.layout.view_tab_text)
                }
                val textView: TextView = tab.customView!!.findViewById(android.R.id.text1)
                textView.setTextAppearance(R.style.TabLayoutTextSize_default)

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
    }

    /**
     * 初始化视频播放器
     */
    private fun initVideoPlayer() {
        player = PlayerView(myActivity)
            .setScaleType(PlayStateParams.fitparent)
            .setShowSpeed(true)
            .forbidTouch(false)
            .hideHideTopBar(true)
            .hideSteam(true)
            .hideFullscreen(false)
            .hideRotation(true)
            .hideBottonBar(false)
    }

    /**
     * 开始播放视频
     */
    private fun startVideoPlayer(mp4: String, bg: String) {
        context?.let {
            player.showThumbnail { ivThumbnail ->
                Glide.with(it)
                    .load(bg)
                    .placeholder(R.color.black)
                    .error(R.color.black)
                    .into(ivThumbnail)
            } .setPlaySource(
                MyApplication.instance().getProxy(it)
                //?.getProxyUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
                ?.getProxyUrl(mp4)
            )
              .startPlay()
        }

    }

    override fun onStop() {
        super.onStop()
        player?.stopPlay()
    }

}